package com.dragon.mmochat.model.role;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.model.enumType.MmochatNpcFaceType;
import com.dragon.mmochat.model.enumType.MmochatNpcItemType;
import com.dragon.mmochat.model.enumType.MmochatRoleBmpType;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.util.MmochatUtil;
import com.google.gson.reflect.TypeToken;

public class MmochatNpc implements Serializable {
	private static final long serialVersionUID = -2795489490908616943L;
	private int npcId;
	private int mapId;
	private short x;
	private short y;
	private String name;
	private MmochatRoleBmpType bmpType;
	protected MmochatNpcFaceType face;

	private List<MmochatNpcItemType> functionItems = new ArrayList<MmochatNpcItemType>();
	Map<Integer, MmochatTask> tasks = new ConcurrentHashMap<Integer, MmochatTask>();
	private List<String> defaultMsg = new ArrayList<String>();

	public MmochatNpc() {
		if (MmochatUtil.isInPercent(50)) {
			face = MmochatNpcFaceType.朝左;
		} else {
			face = MmochatNpcFaceType.朝右;
		}
	}

	public MmochatNpc getClone() {
		return (MmochatNpc) MmochatUtil.deepClone(this);
	}

	public int getNpcId() {
		return npcId;
	}

	public void setNpcId(int npcId) {
		this.npcId = npcId;
	}

	public int getMapId() {
		return mapId;
	}

	public void setMapId(int mapId) {
		this.mapId = mapId;
	}

	public short getX() {
		return x;
	}

	public void setX(short x) {
		this.x = x;
	}

	public short getY() {
		return y;
	}

	public void setY(short y) {
		this.y = y;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Map<Integer, MmochatTask> getTasks() {
		return tasks;
	}

	public void setTasks(Map<Integer, MmochatTask> tasks) {
		this.tasks = tasks;
	}

	public void addTask(MmochatTask task) {
		tasks.put(task.getTaskId(), task);
	}

	public MmochatTask getTask(int taskId) {
		return tasks.get(taskId);
	}

	public List<String> getDefaultMsg() {
		return defaultMsg;
	}

	public void setDefaultMsg(List<String> defaultMsg) {
		this.defaultMsg = defaultMsg;
	}

	public void addDefaultMsg(String newMsg) {
		defaultMsg.add(newMsg);
	}

	public String getDefaultMessage() {
		int msgNum = defaultMsg.size();
		if (msgNum > 0) {
			int index = (int) (Math.random() * msgNum);
			try {
				return defaultMsg.get(index);
			} catch (Exception e) {
				e.printStackTrace();
				return defaultMsg.get(0);
			}
		}
		return "找我有什么事么?";
	}

	public MmochatRoleBmpType getBmpType() {
		return bmpType;
	}

	public void setBmpType(MmochatRoleBmpType bmpType) {
		this.bmpType = bmpType;
	}

	public List<MmochatNpcItemType> getFunctionItems() {
		return functionItems;
	}

	public void setFunctionItems(List<MmochatNpcItemType> functionItems) {
		this.functionItems = functionItems;
	}

	public MmochatNpcFaceType getFace() {
		return face;
	}

	public void setFace(MmochatNpcFaceType face) {
		this.face = face;
	}
	
	public void setRandomFace(){
		if (MmochatUtil.isInPercent(50)) {
			face = MmochatNpcFaceType.朝左;
		} else {
			face = MmochatNpcFaceType.朝右;
		}
	}

	// 任务状态: 0:无；1:接任务；2:交任务
	public int getTaskStateValue(MmochatPlayer me) {
		boolean hasNewTask = false;
		for (MmochatTask task : getTasks().values()) {
			if (task.getFinish_npcId() == getNpcId()
					&& me.canFinishTask(task.getTaskId())) {
				return 2;
			} else if (!hasNewTask && task.getAccept_npcId() == getNpcId()
					&& me.canAcceptTask(task.getTaskId())) {
				hasNewTask = true;
			}
		}
		if (hasNewTask) {
			return 1;
		}
		return 0;
	}

}
